﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System.Data;

#endregion

namespace ScrumTable.Common.Excel.OpenXmlSdk
{
    /// <summary>
    /// Provides the facilities for de-/serialization of an open xml sdk file.
    /// </summary>
    public class OpenXmlWorkbookSerializer : IWorkbookSerializer
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------
        
        internal const string IdColumnName = "ID";

        private readonly string _workBookPath;
        private DataSet _deserializedWorkBook;

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        /// <summary>
        /// Gets the moniker (e.g. file path / uri / ...) to the workbook file.
        /// </summary>
        public string Moniker { get { return _workBookPath; } }

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        /// <summary>
        /// Creates a new OpenXmlWorkbookSerializer instance.
        /// </summary>
        /// <param name="workbookPath">Specifies the path of the workbook to deserialize.</param>
        public OpenXmlWorkbookSerializer(string workbookPath)
        {
            PreCondition.AssertNotNullOrEmpty(workbookPath, "workbookPath");

            _workBookPath = workbookPath;
        }

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Loads the data from the underlying excel xml file.
        /// </summary>
        public DataSet Deserialize()
        {
            return (_deserializedWorkBook = new WorkbookDeserializer(IdColumnName).LoadData(Moniker));
        }

        /// <summary>
        /// Stores the data to the underlying excel xml file.
        /// </summary>
        public void Serialize()
        {
            new WorkbookSerializer(IdColumnName).SaveData(Moniker, _deserializedWorkBook);
        }

        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        #endregion
    }
}